package com.appspot.aliga;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.aliga.openligadb.LeagueTableUpdater;
import com.appspot.aliga.openligadb.OpenLigaDBUpdater;
import com.appspot.aliga.openligadb.service.Sportsdata;
import com.appspot.aliga.openligadb.service.SportsdataSoap;
import com.appspot.aliga.persistence.PersistenceService;

public class UpdateServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String delete = req.getParameter("cleanDB");
		String updateAll = req.getParameter("updateAll");
		String updateLeagueTable = req.getParameter("updateLeagueTable");
		if ("true".equals(delete)) {
			new PersistenceService().cleanDB();
			resp.setContentType("text/plain");
			resp.getWriter().println("cleaned Database");
		} else if ("true".equals(updateAll)) {
			OpenLigaDBUpdater updater = new OpenLigaDBUpdater();
			updater.updateAllMatches();
			resp.setContentType("text/plain");
			resp.getWriter().println("updated all Matches");
		} else if ("true".equals(updateLeagueTable)) {
			LeagueTableUpdater updater = new LeagueTableUpdater();
			updater.updateAllLeagueTable();
			resp.setContentType("text/plain");
			resp.getWriter().println("updated all LeagueTables");
		} else {
			OpenLigaDBUpdater updater = new OpenLigaDBUpdater();
			updater.updateCurrentGameDay();

			resp.setContentType("text/plain");
			resp.getWriter().println("updated current Gameday");
		}
	}
}
